package com.songcha.library_common.ui.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.songcha.library_common.util.sp2px

/***
 * textview will show incomplete when style is italic or custom font,
 * show add 1sp width behind
 */

class AutoWidthItalicTextView(context: Context, attrs: AttributeSet?) : AppCompatTextView(context, attrs) {
    private var mPaint:Paint?=null

    init {
        initView()
    }

    private fun initView(){

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        if(text.toString().isEmpty()) return
        if(mPaint==null){
            mPaint= Paint(Paint.ANTI_ALIAS_FLAG)
            mPaint!!.textSize=textSize
            mPaint!!.isFakeBoldText=typeface.isBold
        }
        val w=mPaint!!.measureText(text.toString())
        width=(w.toInt()+1.sp2px())

        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }


}